# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: daniel

import pandas as pd
import numpy as np
import sys

# read json file
file_name = sys.argv[1]
df = pd.read_json(file_name, orient='records')

ss  = [row['interval'] for _, row in df.iterrows()]
ss  = np.unique(ss)

print("%20s : %20s (ms)" % ("Category(Interval)", "Time Delta(ts2 - ts1)"))
for s in ss:
    # session1
    s1  = [row for _, row in df.iterrows() if row['interval'] == s and row['session'] == 1]
    s1  = sorted(s1, key=lambda x: x['index'])
    ts1 = np.array([each['ts'] for each in s1])
    
    # session2
    s2  = [row for _, row in df.iterrows() if row['interval'] == s and row['session'] == 2]
    s2  = sorted(s2, key=lambda x: x['index'])
    ts2 = np.array([each['ts'] for each in s2])
    
    # mean
    print("%20s : %20s (ms)" % (s, np.median(ts2 - ts1) / 1000000))
